﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestVdataAdapter.TestPreparation
{
    class TestDataFolder
    {
        /// <summary>
        /// Get the test data folder for tests which use file data for simulation
        /// Because each test is run in a new folder with unique ID, hence we have to move up a little bit
        /// in order to reach to the test data folder (in the same level with test results folder)
        /// </summary>
        /// <returns></returns>
        public static string GetTestDataFolder()
        {
            string current = Environment.CurrentDirectory;
            
            //move up 3 levels in a stupid way
            string baseFolder = GetParentFolder(current, 3);

            return baseFolder + @"\TestData";
        }

        /// <summary>
        /// Get parent folder of 'level' levels above
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        private static string GetParentFolder(string currentDirectory, int level)
        {
            try
            {
                for (int i = 0; i < level; i++)
                {
                    currentDirectory = Directory.GetParent(currentDirectory).ToString();
                }
            }
            catch (IOException ex)
            {
                throw new IOException("Error get parent folder: ", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("Unknown exception when trying to get parent folder");
            }

            return currentDirectory;
        }
    }
}
